1228B - Filling the Grid - CodeForces Solution


implementation math *1400

Please click on ads to support us..

Python Code:

h, w = [int(z) for z in input().split()]
r = [int(z) for z in input().split()]
c = [int(z) for z in input().split()]
mod = 10**9 + 7
 
grid = [['?' for col in range(w+1)] for row in range(h+1)]
def try_set(row, col, target):
    if grid[row][col] == '?':
        grid[row][col] = target
    elif grid[row][col] != target:
        raise ValueError
 
def go():
    try:
        for row in range(h):
            for col in range(r[row]):
                try_set(row, col, 'FULL')
            try_set(row, r[row], 'EMPTY')
        for col in range(w):
            for row in range(c[col]):
                try_set(row, col, 'FULL')
            try_set(c[col], col, 'EMPTY')
    except ValueError:
        return 0
    answer = 1
    for row in range(h):
        for col in range(w):
            if grid[row][col] == '?': answer = answer * 2 % mod
    return answer
    
print(go())

C++ Code:

// NGUYEN QUOC HUNG - quochung.cyou

#include <bits/stdc++.h>
using namespace std;

// Template
template <typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for (auto &x : a)
        in >> x;
    return in;
};
template <typename T>
ostream &operator<<(ostream &out, vector<T> &a) {
    for (auto &x : a)
        out << x << ' ';
    return out;
};
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) { return out << x.f << ' ' << x.s; }
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) { return in >> x.f >> x.s; }
template <typename T>
using matrix = vector<vector<T>>;
template <typename T>
using rubik = vector<vector<vector<T>>>;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

typedef long long ll;

typedef long double ld;
#define db(x) cout << (x) << '\n';
#define faster()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);                \
    cout.tie(NULL);
#define FORAB(i, a, b) for (int i = a; i < (b); i++)
#define FOR(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define ull unsigned long long int
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
const int MOD = 1000000007;

ll powMod(ll a, ll b, ll mod = MOD) {
    ll res = 1;
    a %= mod;
    for (; b; b >>= 1) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
    }
    return res;
}

int main() {
    faster();
    int h, w;
    cin >> h >> w;
    int a[h * 2 + 5][w * 2 + 5] = {};
    int yeuban[h * 2 + 5][w * 2 + 5] = {};
    int hang[h + 5];
    int cot[w + 5];
    FOR(i, h) {
        int x;
        cin >> x;
        hang[i] = x;
        FOR(j, x) {
            a[i][j] = 1;
        }
        yeuban[i][x] = 2;

        // a[i][x+1] = 1;
    }
    FOR(i, w) {
        int x;
        cin >> x;
        cot[i] = x;
        FOR(j, x) {
            a[j][i] = 1;
        }
        yeuban[x][i] = 2;
        // a[x+1][i] = 1;
    }

    FOR(i, h) {
        FOR(j, w) {
            if (yeuban[i][j] && a[i][j]) {
                cout << 0 << "\n";
                return 0;
            }
            if (yeuban[i][j] || a[i][j]) {
                a[i][j] = 1;
            }
        }
    }
    int dem = 0;

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            // cout << a[i][j] << " ";
            if (a[i][j] == 0) {
                dem++;
            }
        }
        cout << endl;
    }
    ll kq = powMod(2, dem);
    cout << kq;
}


Comments

Submit
0 Comments
More Questions

1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque